const { getCode, writeOffPic } = require("../../utils/api")

// custom-dialog/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //显示正常弹窗
    show: {
      type: Boolean,
      value: false
    },
    //正常信息
    tipmsg: {
      type: Object,
      value: {},
    },

    //错误信息
    errmsg: {
      type: String,
      value: '',
    },

    //失效弹窗
    showErr: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showPic: false,
    skuDetail: {},
    isJwt: false,
    showLogin: false

  },


  /**
   * 组件的方法列表
   */

  methods: {
    //关闭失效弹窗
    closeErrDialog() {
      this.setData({
        showErr: false
      })
    },


    //再次扫码
    codeBtn() {
      this.setData({
        showErr: false
      })
      //调用副组件扫码方法
      this.triggerEvent("parentFun");
    },


    //关闭正常弹窗
    closeDialog() {
      this.setData({
        show: false
      })
    },

    //领取积分
    getPoint() {
      let that = this



      const jwt = wx.getStorageSync('jwt');
      if (jwt) {
        that.setData({
          isJwt: true
        })
      } else {
        that.setData({
          isJwt: false
        })
      }

      if (that.data.isJwt) {


        that.setData({
          show: false
        })

        setTimeout(function () { that.triggerEvent("parentFun") }, 500);
        setTimeout(function () {
          that.setData({
            showPic: true
          })
        }, 3000);

        let params = {
          goodsurl: that.data.tipmsg.goodsurl
        }
        //走核销接口
        writeOffPic(params).then((res) => {

          if (res.errcode == 0) {
            setTimeout(function () { that.triggerEvent("parentFun") }, 500);
            setTimeout(function () { that.triggerEvent("fatherFun") }, 2000);

            that.setData({
              skuDetail: res.data,
              "skuDetail.huodongnum": res.data.canscore * Number(res.data.multiplier),
              // showPic: true
            })
            setTimeout(function () {
              that.setData({
                showPic: true
              })
            }, 4000);

          } else {
            wx.showToast({
              title: res.errmsg,
              icon: 'none',

            });
          }
        })
      } else {
        that.setData({
          showLogin: true
        })
      }


    },

    //关闭图鉴弹窗
    closePic() {
      this.setData({
        showPic: false
      })
    },


    toShop() {
      wx.switchTab({
        url: '/pages/shoppage/shop',
        success: (result) => {
          this.setData({
            showPic: false
          })

        },

      });


    },

    toPicDetail() {
      wx.navigateTo({
        url: `/packageA/pages/picdetail/index?skuid=${this.data.skuDetail.seriessku.series_num}`,
        success: (result) => {
          this.setData({
            showPic: false
          })
        },
      });
    }

  }
})
